package com.mystnihon.demoftp;

import java.io.File;

import org.apache.commons.net.ftp.FTPFile;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.mystnihon.demoftp.ftp.FTPManager;
import com.mystnihon.demoftp.ftp.FTPManager.FTPListener;

public class MainActivity extends Activity implements View.OnClickListener {
    private FTPManager mFTPManager;

    private Button mConnect;
    private TextView mData;;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mConnect = (Button) findViewById(R.id.connect);
        mData = (TextView) findViewById(R.id.data);

        mConnect.setOnClickListener(this);

        mFTPManager = new FTPManager(getApplicationContext());
        mFTPManager.setFTPListener(new FTPListener() {

            @Override
            public void onFiles(FTPFile[] files, String directory) {
                StringBuilder builder = new StringBuilder();
                for (FTPFile ftpFile : files) {
                    builder.append(String.format("Filename:  %s, Directory: %s, Path %s\n", ftpFile.getName(), ftpFile.isDirectory() ? "yes" : "no", ftpFile.getLink()));
                }
                mData.append(builder.toString());
            }

            @Override
            public void onDownloadFinished(String file, int downloadPos) {

                mData.append("Download finished : " + file);
            }

            @Override
            public void onDownloadError(int errorCode, String downloadFailed, int downloadPos) {
                mData.append("Download error : " + downloadFailed);
            }

            @Override
            public void onDisconnected() {

            }

            @Override
            public void onDirectories(FTPFile[] files, String directory) {
                StringBuilder builder = new StringBuilder();
                for (FTPFile ftpFile : files) {
                    builder.append(String.format("Filename:  %s, Link: %s\n", ftpFile.getName(), ftpFile.getLink()));
                }

                mData.append(builder.toString());
            }

            @Override
            public void onDownloadProgress(String file, long byteTransferred, int downloadPos) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onConnect(boolean connected) {
                if (connected) {
                    mData.append("Connected\n");
                    mData.append("Loging with user seedbox4e91b421b51fb\n");
                    mFTPManager.login("seedbox4e91b421b51fb", "YkwLLa6hN7");
                } else {
                    mData.append("Not Connected\n");
                }

            }

            @Override
            public void onAuthenticated(boolean success) {
                if (success) {
                    mData.append("Logged\n");
                    downloadFTPFile("Aspen/Aspen - T02.cbr");
                } else {
                    mData.append("Login Failed\n");
                }
            }

        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {

        super.onPause();
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();

    }

    @Override
    protected void onStop() {
        mData.append("Disconnect");
        mFTPManager.disconnect();
        super.onStop();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void onClick(View v) {
        mData.append("Connecting to front73.seedbox.co\n");
        mFTPManager.connect("front73.seedbox.co", 21);

    }

    private void downloadFTPFile(String ftpAddress) {// "Aspen/Aspen - T01.cbr"

        File defaultDownloadDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        Uri uri = Uri.parse("file://" + defaultDownloadDirectory + "/" + ftpAddress);

        String directoryToDownload = uri.getPath().replace(uri.getLastPathSegment(), "");// "Aspen";
        String fileName = uri.getLastPathSegment();// "Aspen - T01.cbr";
        Log.d("", String.format("Directory Remote %s", directoryToDownload));
        Log.d("", String.format("Filename %s", fileName));

        File directoryToDownloadFile = new File(directoryToDownload);
        if (!directoryToDownloadFile.exists()) {
            directoryToDownloadFile.mkdirs();
        }
        if (directoryToDownloadFile.exists()) {
            File file = new File(directoryToDownloadFile, fileName);

            mFTPManager.download(ftpAddress, file);
        }
    }

}
